package cn.sz.hy.common;

import java.util.Hashtable;

import org.eclipse.swt.widgets.Widget;

import cn.sz.hy.event.Event;
import cn.sz.hy.handler.Handler;
import cn.sz.hy.logic.strategy.vo.TableVo;

@SuppressWarnings("unchecked")
public class EventBus {

	private final Hashtable<Widget, Handler> mapping = new Hashtable<Widget, Handler>();
	private final Hashtable<Integer, Object> ComponentBus = new Hashtable<Integer, Object>();
	private final Hashtable<Integer, TableVo> tableVoBus = new Hashtable<Integer, TableVo>();
	
	private static final EventBus eBus = new EventBus();

	private EventBus() {

	}

	public static EventBus getInstance() {
		return eBus;
	}

	public void execute(Event event) {
		Handler<Event> handler = mapping.get(event.getKey());
		if (handler != null) {
			handler.execute(event);
		}
	}

	public void addHandler(Widget key, Handler<? extends Event> handler) {
		mapping.put(key, handler);
	}

	public void addComponent(Integer id, Object cop) {
		ComponentBus.put(id, cop);
	}

	public Object getComponent(Integer id) {
		return ComponentBus.get(id);
	}
	
	public void addTableVo(Integer key, TableVo vo) {
	    tableVoBus.put(key, vo);
    }
	
	public TableVo getTableVo(Integer key) {
        return tableVoBus.get(key);
    }

}
